package com.zrrd.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.zrrd.common.bo.RuleBo;
import com.zrrd.common.vo.RulePageVo;
import com.zrrd.common.vo.RuleVo;
import com.zrrd.entity.Rule;
import com.zrrd.mapper.RuleMapper;
import com.zrrd.service.RuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-09-09
 */
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements RuleService {
    @Resource
    private RuleMapper ruleMapper;

    /**
     * 根据角色id查询权限列表并分级
     * @param roleId
     * @return
     */
    @Override
    public List<RuleBo> getRuleListByRoleId(Integer roleId) {
        List<Rule> list = ruleMapper.getRuleListByRoleId(roleId);

        //JDK1.8之后新增lambda、也新增了集合的stream流
        List<RuleBo> ruleBoList = list.stream().map(rule -> {
            RuleBo ruleBo = new RuleBo();
            BeanUtils.copyProperties(rule, ruleBo);
            return ruleBo;
        }).collect(Collectors.toList());

        //重新遍历集合 递归
        List<RuleBo> ruleBos = interatorRuleList(ruleBoList, 0);

        return ruleBos;
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public RulePageVo findByPage(Integer page) {
        RulePageVo rulePageVo = new RulePageVo();
        List<RuleBo> ruleBos = list().stream().map(rule -> {
            RuleBo ruleBo = new RuleBo();
            BeanUtils.copyProperties(rule, ruleBo);
            return ruleBo;
        }).collect(Collectors.toList());

        List<RuleBo> ruleBoList = interatorRuleList(ruleBos, 0);

        rulePageVo.setList(ruleBoList);

        //查询所有权限列表
        List<RuleVo> ruleVoList = list().stream().map(rule -> {
            RuleVo ruleVo = new RuleVo();
            BeanUtils.copyProperties(rule, ruleVo);
            ruleVo.setLevel();
            return ruleVo;
        }).collect(Collectors.toList());

        rulePageVo.setRules(ruleVoList);

        rulePageVo.setTotalCount(Long.valueOf(ruleBoList.size()));

        return rulePageVo;
    }

    /**
     * 私有方法，利用递归完成分级遍历
     * @param list RuleBod的集合
     * @param pId  每一个菜单的上一级ID parentId
     * @return
     */
    private List<RuleBo> interatorRuleList(List<RuleBo> list,Integer pId) {
        //创建一个新集合
        List<RuleBo> ruleBoList = new ArrayList<>();

        list.forEach(ruleBo -> {
            //设置权限菜单记录的id
            Integer id = ruleBo.getId();
            //设置权限菜单记录的父id
            Integer parentId = ruleBo.getRuleId();
            if (parentId.equals(pId)) {
                //递归调用，调用自己的方法
                List<RuleBo> childList = interatorRuleList(list, id);
                //设置子菜单
                ruleBo.setChild(childList);
                //把设置后的子菜单放到新集合
                ruleBoList.add(ruleBo);
            }

        });

        return ruleBoList;
    }



}
